package cn.edu.scnu.semart.order.mapper;
import cn.edu.scnu.semart.model.order.PaymentRecord;
import org.apache.ibatis.annotations.*;

@Mapper
public interface PaymentRecordMapper {

    @Insert("INSERT INTO payment_record (order_number, amount, payment_method, transaction_id, status, create_time) " +
            "VALUES (#{orderNumber}, #{amount}, #{paymentMethod}, #{transactionId}, #{status}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "paymentId")
    int insert(PaymentRecord record);

    @Select("SELECT * FROM payment_record WHERE order_number = #{orderNumber}")
    PaymentRecord findByOrderNumber(@Param("orderNumber") String orderNumber);

    @Update("UPDATE payment_record SET status = #{status}, transaction_id = #{transactionId}, complete_time = NOW() " +
            "WHERE order_number = #{orderNumber}")
    int updateStatus(@Param("orderNumber") String orderNumber,
                     @Param("status") String status,
                     @Param("transactionId") String transactionId);

    @Update("UPDATE payment_record SET status = #{status}, complete_time = NOW() " +
            "WHERE order_number = #{orderNumber}")
    int updateStatusWithoutTransaction(@Param("orderNumber") String orderNumber,
                                       @Param("status") String status);
}